﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System.Xml.Serialization;

namespace ConfigGen.Core
{
    /// <summary>
    /// This class encapsulates all run-time options and settings for the config gen application
    /// </summary>
    public class Preferences
    {
        /// <summary>
        /// Gets or sets a flag indicating whether to inhibit actual writing of generated configuration files. If true, the entire config gen process
        /// will execute without any actual files having been written.
        /// </summary>
        public bool InhibitWrite { get; set; }

        /// <summary>
        /// Gets or sets the path to the settings spreadsheet file.
        /// </summary>
        public string SettingsFile { get; set; }

        /// <summary>
        /// Gets or sets the path to the template xml file.
        /// </summary>
        public string TemplateFile { get; set; }

        /// <summary>
        /// Gets or sets the output directory into which to write the generated files.
        /// </summary>
        public string OutputDirectory { get; set; }

        /// <summary>
        /// Gets or sets a flag indicating that the default behaviour of "one config file per subdirectory" should be disbaled, and all generated files should be
        /// placed directly in the directory specified by <see cref="OutputDirectory"/>.
        /// </summary>
        /// <remarks><para>By default, a subdirectory with the name of the machine is created inside the directory specified by <see cref="OutputDirectory"/> for
        /// each machine whose config is being generated. It is into this that the generated file is placed.</para>
        /// <para>By setting this flag to true, this behaviour is inhibited, and all generated files are placed directly in the output directory.</para>
        /// <para><strong>WARNING: </strong>When this option is set, configuration files will overwrite eachother if they have the same names, unless 
        /// <see cref="MachineSuffix"/> is also enabled.</para></remarks>
        public bool FlatDirectoryOutput { get; set; }

        /// <summary>
        /// Gets or sets a flag indicating that each generated file should be suffixed with the machine name. e.g. an App.Config for "DevSrv1" would be named App.Config.DevSrv1.
        /// </summary>
        public bool MachineSuffix { get; set; }

        /// <summary>
        /// Gets or sets a flag indicating that the generated xml files should be "pretty print" formatted.
        /// </summary>
        /// <remarks>
        /// <para><b>NOTE: this setting has been deprecated and should now be specified in the template preferences section of the actual configuration template. Setting
        /// this value here will <em>override</em> the template setting.</b></para>
        /// <para>During file generation, some whitespace, in particular intra-element whitespace, is lost. This is due to the fact that the xml is parsed 
        /// into a DOM and back. For heavily attributised configurations this can cause difficult-to-read configuration files. In these cases, it is recommended that
        /// this option is enabled.</para>
        /// </remarks>
        public bool PrettyPrint { get; set; }

        /// <summary>
        /// Gets or sets the maximum line length to allow when pretty printing. Must be used in conjunction with <see cref="PrettyPrint"/> being set.
        /// </summary>
        /// <remarks>
        /// <para><b>NOTE: this setting has been deprecated and should now be specified in the template preferences section of the actual configuration template. Setting
        /// this value here will <em>override</em> the template setting.</b></para>
        /// </remarks>
        public int? PrettyPrintLineLength { get; set; }

        /// <summary>
        /// Gets or sets a flag indicating that verbose output should be written.
        /// </summary>
        public bool Verbose { get; set; }

        /// <summary>
        /// Gets or sets a flag indicating that only the configuration file for the local machine should be generated.
        /// </summary>
        /// <remarks><para>When set, the generator will look for a row in the settings spreadsheet whose machine name is equal to the current machine name, and
        /// only this file will be generated. If such a machine is not found, a row for a machine named "default" will be used instead.</para></remarks>
        public bool LocalOnly { get; set; }

        /// <summary>
        /// Gets or sets the forced filename. If specified, this filename overrides the filename specified in the settings spreadsheet.
        /// </summary>
        public string ForcedFilename { get; set; }

        /// <summary>
        /// Gets or sets an array of machines for which to generate configuration files.
        /// </summary>
        public string[] SpecifiedMachines{ get; set; }

        /// <summary>
        /// Gets or sets a Regex filter to identify machines for which to generate configuration files.
        /// </summary>
        public string MachineFilterExpression { get; set; }

        /// <summary>
        /// Gets or sets a flag specifying that configuration files should be written out, even if their content has not changed. This means the 
        /// timestamp on the file will be updated.
        /// </summary>
        public bool WriteUnchangedFiles { get; set; }

        /// <summary>
        /// Gets or sets a flag that, if true, causes the application to write it's configuration file to a file specified by the <see cref="DumpSettingsFile"/> setting. configGenDumpedSettings.xml and then exit.
        /// </summary>
        public bool DumpSettingsOnly { get; set; }

        /// <summary>
        /// Gets or sets a filename into which the ConfigGen settings should be dumped. This setting is ignored unless <see cref="DumpSettingsOnly"/> is also specified.
        /// </summary>
        public string DumpSettingsFile { get; set; }

        /// <summary>
        /// Gets or sets a flag instructing ConfigGen to remove the root xml element from the generated output files. This is useful when generating non xml output files.
        /// </summary>
        public bool RemoveRootElement { get; set; }

        /// <summary>
        /// Gets or sets the path to the default settings spreadsheet file.
        /// </summary>
        public string DefaultSettingsFile { get; set; }

        /// <summary>
        /// Gets or sets the output encoding of the file.
        /// </summary>
        public string OutputEncoding { get; set; }

        /// <summary>
        /// Converts an object into its XML representation.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Xml.XmlWriter"/> stream to which the object is serialized. </param>
        public virtual void SerializeToXml(System.Xml.XmlWriter writer)
        {
            var serializer = new XmlSerializer(typeof (Preferences));
            serializer.Serialize(writer, this);
        }
    }
}
